package com.yyds.yygh.user.controller.api;

import com.yyds.yygh.common.result.Result;
import com.yyds.yygh.common.utils.AuthContextHolder;
import com.yyds.yygh.model.user.Patient;
import com.yyds.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/api/user/patient")
@Api(tags = "就诊人管理")
public class PatientApiController {
    @Autowired
    private PatientService patientService;

    @ApiOperation("根据id查询就诊人列表")
    @GetMapping("/auth/findAll")
    public Result findAll(HttpServletRequest request){
        //获取当前登录用户i
        Long userId = AuthContextHolder.getUserId(request);
        //获取当前用户下的就诊人列表
        List<Patient> patients = patientService.findAllByUserId(userId);
        return Result.success(patients);
    }
    @ApiOperation("添加就诊人")
    @PostMapping("/auth/save")
    public Result savePatient(@RequestBody Patient patient , HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.success();
    }
    @ApiOperation("根据id获取就诊人信息")
    @GetMapping("/auth/get/{id}")
    public Result getPatient(@PathVariable Long id){
        Patient patient = patientService.getPatientById(id);
        return Result.success(patient);
    }
    @ApiOperation("根据id获取就诊人信息共内部调用")
    @GetMapping("/inner/get/{id}")
    public Patient getPatientById(@PathVariable long id){
        return patientService.getPatientById(id);
    }
    @ApiOperation("修改就诊人")
    @PutMapping("/auth/update")
    public Result updatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return  Result.success();
    }
    @ApiOperation("删除就诊人")
    @DeleteMapping("/auth/remove/{id}")
    public Result removePatient(@PathVariable Long id){
        patientService.removeById(id);
        return Result.success();
    }
}
